String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * 鼠标离开校验
 *
 */
function rtCheck(validator){
	if(validator.element){
		$(validator.element).blur(validate);
	}
	function validate(){
		if(!validator || !validator.element){
			return;
		}
		if(!validator.validate()){
			if(!validator.errorEl){
				alert(validator.errorMsg);
			}else{
				validator.errorEl.style.color='red';
				validator.errorEl.innerHTML = " "+validator.errorMsg;
			}	
		}else{
			if(validator.errorEl){
				var realErrorMsg = validator.errorEl.innerHTML.trim();
				var defineErrorMsg = validator.errorMsg.trim();
				if(realErrorMsg != '' && realErrorMsg==defineErrorMsg){
					validator.errorEl.innerHTML = "";
				}
			}
		}
	}	
}

/**
 * 非空校验
 */
function NullValidator(inputEl, errorMsg, errorEl, isRTCheck){
	this.element = inputEl;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	this.isRTCheck = isRTCheck;	
	if(isRTCheck){
		rtCheck(this);
	}
	$(inputEl).after('<span style="color:red"> *</span>');
}
NullValidator.prototype.validate = function(){
	if(this.element.value.trim() == ""){		
		return false;
	}	
	return true;
}

/**
 * 匹配校验
 */
function PatternValidator(inputEl, pattern, errorMsg, errorEl, isRTCheck){
	this.element = inputEl;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	this.pattern = pattern;
	if(isRTCheck){
		rtCheck(this);	
	}
}
PatternValidator.prototype.validate = function(){
	if(this.element.value.match(this.pattern) == null){
		return false;
	}
	return true;
}

/**
 * 字符长度校验
 */
function CharacterLengthValidator(inputEl, length, errorMsg, errorEl, isRTCheck){
	this.element = inputEl;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	this.length = length;
	if(isRTCheck){
		rtCheck(this);	
	}
}
CharacterLengthValidator.prototype.validate = function(){
	return this.element.value.trim().length < this.length;
}

/**
 * 用户名存在校验
 */
function AccountValidator(elementEl, url, errorMsg, errorEl, isRTCheck){
	this.element = elementEl;
	this.url = url;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	if(isRTCheck){
		rtCheck(this);
	}
}
AccountValidator.prototype.validate = function(){
	var isSuccess = true;
	$.ajax({
  	  type: "POST",
  	  url: this.url,
  	  async: false,
  	  data:this.element.name+'='+this.element.value+'&random='+Math.random(),
  	  dataType: "json",
  	  complete:function(response){
        var data = response.responseText;
        var json = eval("("+data+")");
  	    if(!json.success){
  	    	$(this.errorMsg).html(json.message);
  	    	isSuccess = json.success;
  	    	
	    }
      }
    });
	return isSuccess;
}

/**
 * 密码一致性校验
 */
function PasswordConsistValidator(passwordEl, repasswordEl, errorMsg, errorEl, isRTCheck){
	this.element = passwordEl;
	this.repasswordEl = repasswordEl;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	if(isRTCheck){
		rtCheck(this);	
	}
}
PasswordConsistValidator.prototype.validate = function(){
	return this.element.value.match(this.repasswordEl.value) != null;
}

/**
 * 密码长度校验
 */
function PasswordLengthValidator(password, startLength, errorMsg, errorEl, isRTCheck){
	this.element = password;
	this.sLength = startLength;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	if(isRTCheck){
		rtCheck(this);	
	}
}
PasswordLengthValidator.prototype.validate = function(){
	if(!this.element.value){
		return false;
	}
	return this.element.value.trim().length >= this.sLength; 
}

/**
 * 手机号码和传真号校验.二者必有其一
 */
function PhoneNumberFaxValidator(faxNum, phoneNum, errorMsg, errorEl, isRTCheck){
	this.element = faxNum;
	this.faxNum = phoneNum;
	this.errorMsg = errorMsg;
	this.errorEl = errorEl;
	if(isRTCheck){
		rtCheck(this);	
	}
}
PhoneNumberFaxValidator.prototype.validate = function(){
	var faxNum = this.element.value.trim();
	var phoneNum = this.faxNum.value.trim();
	if(!phoneNum && !faxNum){
		return false;
	}
	return true;
}

function addSymbol(inputEl){
	$(inputEl).after('<span></span>');
	$(inputEl).after($('<span style="color:red"> *</span>'));
}


/**
 * 表单提交校验
 */
function formSubmitRequest(event,form){
	var isPass = true;
	var validators = form.validators;
	if(! validators){
		return true;
	}
	
	for(var index=0; index<validators.length; index++){
		var validator = validators[index];
		if(!validator || !validator.element){
			continue;
		}
		
		if(!validator.validate()){	
			if(!validator.errorEl){	
				if(event!=window){
					alert(validator.errorMsg);
				}
			}else{
				validator.errorEl.style.color='red';
				validator.errorEl.innerHTML = " "+validator.errorMsg;
			}
			isPass = false;
			break;
		}else{
			if(validator.errorEl){
				validator.errorEl.innerHTML = "";
			}
		}
	}
	//阻止表单提交事件
	if(!isPass){
		if (!event) {
			return;
		}
		if (event.stopPropagation) {
			event.stopPropagation();
		} else {
			event.cancelBubble = true;
		}
		if (event.preventDefault) {
			event.preventDefault();
		} else {
			event.returnValue = false;
		}
		return false;
	}else{
		return true;
	}
}

function commonSelect(selectEl, id){
	var options = selectEl.options;
	for(opIndex in options){
		var option = options[opIndex];
		if(option && option.selected){
			var measuringUnitContent = document.getElementById(id);
			measuringUnitContent.value = option.text;
			return;
		}
	}
}